﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECI.Entities;
using ECI.CommonLibrary;
using ECI.BaseLanguage;
using ECI.CoreLibrary;

namespace ECI.DynamicData.Data
{
    public class DataModel
    {
        public static EciTable GetDataModel(string code)
        {
            try
            {
                string lanauge = UserInfo.Language;
                EciTable table = new EciTable(EciTableEnum.Code);
                table.Code = code;
                table = table.FetchEntity();

                if (UserInfo.Language == "EN") table.FmainAlias = table.FmainAliasEn;
                if (UserInfo.Language == "TW") table.FmainAlias = table.FmainAliasTw;

                if (table.HasFetchData)
                {
                    EciField field = new EciField(EciFieldEnum.TableGuid);
                    field.TableGuid = table.Guid;
                    field.SortByField = EciFieldEnum.QuerySeq;
                    List<EciField> listFields = field.FetchEntities();

                    foreach (EciField item in listFields)
                    {
                        item.EciTable = table;

                        //改变MailAlias属性
                        if (lanauge == "EN") item.MainAlias = item.MainAliasEn;
                        if (lanauge == "TW") item.MainAlias = item.MainAliasTw;

                        if (item.BatchUpdateShow == 1) table.ListBatchEdit.Add(item);
                        if (item.PrimaryKey == 1) table.PrimaryKey = item;
                        if (item.SumField == 1) table.ListSum.Add(item);
                        if (item.GridShow == 1 && item.LinkUrl.ToMyString() != "") table.ListShowLink.Add(item);
                        if (item.GridShow == 1 && item.DictionaryTypeConfig.ToUpper().StartsWith("HTML")) table.ListShowHTML.Add(item);
                        if (item.GridShow == 1 && item.ShowTip == "Y") table.ListShowTIP.Add(item);
                    }

                    var queryShowItems = from c in listFields where c.QueryShow.ToMyString() == "1" orderby c.QueryShowCommon descending, c.QuerySeq select c;
                    table.ListField = queryShowItems.ToList<EciField>();

                    var gridShowItems = from c in listFields where c.GridShow.ToMyString() == "1" orderby c.GridSeq select c;
                    table.ListShow = gridShowItems.ToList<EciField>();


                    var editShowItems = from c in listFields where c.EditShow.ToMyString() == "1" orderby c.EditSeq select c;
                    table.ListEdit = editShowItems.ToList<EciField>();

                    #region 获取 ECI_TABLE_CONTROL 扩展控件
                    EciTableControl tableControl = new EciTableControl();
                    tableControl.StartSetSQLValue();
                    tableControl.Status = "Y";
                    tableControl.PageCode = code;
                    tableControl.SortByField = EciTableControlEnum.Seq;
                    table.ListControl = tableControl.FetchEntities();
                    #endregion

                    #region 获取ECI_FIELD_EXTEND
                    EciFieldExtend fieldExtend = new EciFieldExtend(EciFieldExtendEnum.TableName);
                    fieldExtend.TableName = table.Fname;
                    table.ListFieldExtend = fieldExtend.FetchEntities();
                    #endregion

                    #region 获取ECI_TABLE_FUNCTION

                    EciTableFunction function = new EciTableFunction(EciTableFunctionEnum.TableName);
                    function.TableName = code;

                    function.SortByField = EciTableFunctionEnum.DisplaySeq;
                    table.ListFunction = function.FetchEntities();

                    foreach (EciTableFunction item in table.ListFunction)
                    {
                        if (lanauge == "EN") item.FunctionName = item.FunctionNameEn;
                        if (lanauge == "TW") item.FunctionName = item.FunctionNameTw;
                    }

                    #endregion

                    #region 获取 ECI_TABLE_DATA_SOURCE 数据源
                    EciTableDataSource tableDataSource = new EciTableDataSource();
                    tableDataSource.StartSetSQLValue();
                    tableDataSource.Status = "Y";
                    tableDataSource.PageCode = code;
                    table.ListDataSource = tableDataSource.FetchEntities();
                    #endregion

                    #region 获取 ECI_TABLE_CONFIRM 数据源
                    EciTableConfirm tableConfirm = new EciTableConfirm();
                    tableConfirm.StartSetSQLValue();
                    tableConfirm.PageCode = code;
                    table.ListConfirm = tableConfirm.FetchEntities();
                    #endregion

                    #region 获取 ECI_TABLE_GROUP 编辑分组信息
                    EciTableGroup tableGroup = new EciTableGroup();
                    tableGroup.StartSetSQLValue();
                    tableGroup.PageCode = code;
                    tableGroup.SortByField = EciTableGroupEnum.Seq;
                    table.ListGroup = tableGroup.FetchEntities();
                    table.ListGroup.Insert(0, new EciTableGroup() { GroupCode = "", GroupName = "" });
                    #endregion
                }
                return table;
            }
            catch (Exception ex)
            {
                //throw new Exception("创建数据字典" + code + "异常:" + ex.Message);
                throw new Exception(Lang.A00053.Read() + code + Lang.A00054.Read() + ":" + ex.Message);
            }
        }
    }
}
